/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.BankDAO;
import com.ivanbiz.dao.MuridDAO;
import com.ivanbiz.dao.impl.BankDAOImpl;
import com.ivanbiz.dao.impl.MuridDAOImpl;
import com.ivanbiz.model.Bank;
import com.ivanbiz.model.Murid;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author IW20149X
 */
public class MuridActionService extends SelectorComposer<Component> {

    MuridDAO muridDAO;
    BankDAO bankDAO;
    @Wire
    Window winMuridAction;
    @Wire
    Textbox nama;
    @Wire
    Combobox listBank;
    @Wire
    Textbox hp;
    @Wire
    Textbox email;
    @Wire
    Textbox alamat;
    Murid murid;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        muridDAO = new MuridDAOImpl();
        bankDAO = new BankDAOImpl();
        murid = (Murid) Executions.getCurrent().getArg().get("murid");
        loadBank();
    }

    @Listen("onClick=#simpan;")
    public void simpan() {
        if (murid == null) {
            murid = new Murid();
        }
        murid.setNama(nama.getValue());
        murid.setBank((Bank) (listBank.getSelectedItem() == null ? null : listBank.getSelectedItem().getValue()));
        murid.setHandphone(hp.getValue());
        murid.setEmail(email.getValue());
        murid.setAlamat(alamat.getValue());
        murid.setNIM("");
        murid.setDate(new Date());
        murid.setTelp(hp.getValue());
        validateMurid(murid);
    }

    private void validateMurid(Murid murid) {
        if (murid.getNama().trim().isEmpty()) {
            Messagebox.show("Nama tidak boleh kosong");
        } else if (murid.getBank() == null) {
            Messagebox.show("Bank tidak boleh kosong");
        } else {
            try {
                muridDAO.saveOrUpdate(murid);
                winMuridAction.detach();
            } catch (Exception ex) {
                Logger.getLogger(PengajarActionService.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void loadBank() {
        try {
            List<Bank> loadBank = bankDAO.getAll(Bank.class);
            for (Bank bank : loadBank) {
                Comboitem item = new Comboitem();
                item.setValue(bank);
                item.setLabel(bank.getNama());
                listBank.appendChild(item);
            }
        } catch (Exception ex) {
            Logger.getLogger(MuridActionService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
